package com.letv.download.cpinfo;

import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letv.download.DownloadResult;

/**
 * data download worker for cp info
 */
public class CpInfoDataDownloadWorker implements Callable<DownloadResult> {

    /**
     * Logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(CpInfoDataDownloadWorker.class);

    /**
     * int, index in the worker list.It's sign of this worker
     */
    private int listIndex;

    /**
     * int, not include this id when select from data base
     */
    private int beginId;

    /**
     * int, include this id when select from data base
     */
    private int endId;

    /**
     * 
     * @param listIndex int, index in the worker list.It's sign of this worker
     * @param beginId int, not include this id when select from data base
     * @param endId int, include this id when select from data base
     */
    public CpInfoDataDownloadWorker(int listIndex, int beginId, int endId) {
        this.listIndex = listIndex;
        this.beginId = beginId;
        this.endId = endId;
    }

    /**
     * call
     * @return DownloadResult, this worker's data download infomation.return null while has errors
     * @throws Exception
     */
    @Override
    public DownloadResult call() throws Exception {
        LOGGER.info("worker " + this.listIndex + " for cp_info begin to download,beginId=" + this.beginId + "(not include),endId=" + this.endId + "(include)");
        return this.downloadToLocal();
    }

    /**
     * download data into local database
     * @return DownloadResult, this worker's data download infomation.return null while has errors
     */
    private DownloadResult downloadToLocal() {
        return new CpInfoDataDownloadTask().downloadToLocalBatch(this.listIndex, this.beginId, this.endId);
    }
}
